package db;





import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import util.DateUtil;
import util.JdbcUtil;
import util.SqlUtil;

public class LogDao {
        
	
	/**
	 * 添加记录
	 * @param path
	 * @return
	 */
	    public boolean insert(String path,long fileid){
	    	Connection conn=JdbcUtil.getConn();
	        Integer id=SqlUtil.getNextId(conn, "filelog", "log_id");
	       String date=DateUtil.getDate();
	    	String sql="insert into filelog values("+id+",'"+path+"',to_date('"+date+"','YY-MM-dd'),"+fileid+")";
	    	Statement stmt=	JdbcUtil.getStmt(conn);
	    boolean b=	JdbcUtil.executeUpdate_Delete_Insert(sql, stmt);
	    if(b){
	    	return true;
	    }
			return false;
	    	
	    }
	    
	    /**
	     * 删除记录
	     * @param id
	     * @return
	     */
	    public boolean delect(long id){
	    	Connection conn=JdbcUtil.getConn();
	       
	    	String sql="delete from filelog where fileid="+id;
	    	System.out.println(sql);
	    	Statement stmt=	JdbcUtil.getStmt(conn);
	    boolean b=	JdbcUtil.executeUpdate_Delete_Insert(sql, stmt);
		return b;
	    }
	    
	    /**
	     * 删除30天前的记录
	     * @return
	     */
	    public boolean delect(){
	    	Connection conn=JdbcUtil.getConn();
	    	String date=DateUtil.getBeforeDay(30);
	    	String sql="delete from filelog where log_date<= to_date('"+date+"','YY-MM-dd')";
	    	System.out.println(sql);
	    	Statement stmt=	JdbcUtil.getStmt(conn);
		    boolean b=	JdbcUtil.executeUpdate_Delete_Insert(sql, stmt);
			return b;
	    }
	    
	    /**
	     * 通过ID查询文件存放位置
	     * @param id
	     * @return
	     */
	   public FileLogPO query(Long id){
		   Connection conn=JdbcUtil.getConn();
	       
	    	String sql="select * from filelog where fileid="+id;
	    	
	    	System.out.println(sql);
	    	Statement stmt=	JdbcUtil.getStmt(conn);
	    	ResultSet rs=JdbcUtil.executeSelect(sql, stmt);
	    	try {
				if(rs.next()){
					FileLogPO log=new FileLogPO();
					log.setId(rs.getInt("log_id"));
					log.setPath(rs.getString("filepath"));
					log.setSourceid(rs.getLong("fileid"));
					return log;
				}

			} catch (SQLException e) {
				return null;
				
			}
		return null;
		   
	   }
	    public static void main(String[] args) {
			LogDao log=new LogDao();
//		    log.delect();
			log.query((long)1243567890);
		}
	    
}
